<?php
/** 
 *  Created by liecol on 2015/4/08.
 */
class WithdrawPayAction extends Action {

		/**
		 * [getwithdrawpayList 提现列表]
		 * @param  [type]  $name      [密友圈名称]
		 * @param  [type]  $nickName  [用户昵称]
		 * @param  [type]  $startTime [开始时间]
		 * @param  [type]  $endTime   [结束时间]
		 * @param  [type]  $state     [状态]
		 * @param  integer $start     [偏移量]
		 * @param  integer $limit     [截取个数]
		 * @return [type]             [description]
		 */
		public function getwithdrawpayList($name=NUll,$nickName=NULL,$startTime=NULL,$endTime=NULL,$state=NULL,$start=0,$limit=10,$isExport = NULL){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			Session::switchCustomer(0);
			$MiconisTxModel = M("miconis_tixian");
			if (!empty($name) && $name !=='') {
			$where['myq.name'] = array('like', "%$name%");
			}
			if (!empty($nickName) && $nickName !=='') {
			$where['qu.nickName'] = array('like', "%$nickName%");
			}
			if ($state!==NUll && $state!=='') {
			$where['tx.state'] = $state;
			}
			if ($startTime != '' && $endTime != '') {
				$where['tx.createTime'] = array(array('egt',$startTime),array('elt',$endTime));
			} else if ($startTime != '') {
				$where["tx.createTime"] = array('egt', $startTime);
			} else if ($endTime != '') {
				$where["tx.createTime"] = array('elt', $endTime);
			}
			if ($isExport) {
				$arr = $MiconisTxModel->alias('tx')
					->join("LEFT JOIN  wx_quan_user as qu ON qu.uid = tx.uId and qu.qid = tx.quanId")
					->join("LEFT JOIN  wx_miyouquan as myq ON tx.quanId = myq.id")
					->where($where)
					->field("myq.name,qu.nickName,qu.headImgUrl,qu.miCoins,tx.cash,tx.state,tx.createTime,tx.id")
					->order('tx.createTime desc')
					->select();
				foreach ($arr as $key => $value) {
					if ($value['state'] == 1) {
						$arr[$key]['state'] = '已提现';
					}else{
						$arr[$key]['state'] = '提现中';
					}
				}
					$expCellName  = array(
						array('name','商品名称'),
						array('nickName','密友'),
						array('cash','金额'),
						array('miCoins','余额'),
						array('state','状态'),
						array('createTime','申请时间'),
					);
					Output::instance()->objArrayExcel('密友提现表', $expCellName, $arr);
				}else{
					$arr = $MiconisTxModel->alias('tx')
						->join("LEFT JOIN  wx_quan_user as qu ON qu.uid = tx.uId and qu.qid = tx.quanId")
						->join("LEFT JOIN  wx_miyouquan as myq ON tx.quanId = myq.id")
						->where($where)
						->limit($start,$limit)
						->field("myq.name,qu.nickName,qu.headImgUrl,qu.miCoins,tx.cash,tx.state,tx.createTime,tx.id")
						->order('tx.createTime desc')
						->select();
				}
			if ($arr) {
				$count = $MiconisTxModel->alias('tx')
					->join("LEFT JOIN  wx_quan_user as qu ON qu.uid = tx.uId and qu.qid = tx.quanId")
					->join("LEFT JOIN  wx_miyouquan as myq ON tx.quanId = myq.id")
					->where($where)
					->field("myq.name,qu.nickName,qu.headImgUrl,qu.miCoins,tx.cash,tx.state,tx.createTime,tx.id")
					->order('tx.createTime desc')
					->count();
				Output::instance()->objArray($arr,$count);
			}else{
				Output::instance()->resultCode(code_success);
			}
		}

		/**
		 * [savewithdrawpaymsg 密友币提现(有事务操作)]
		 * @param  [type] $id [提现表id]
		 * @return [type]     [description]
		 */
		public function savewithdrawpaymsg($id){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$cid = Session::$user["customerId"];
			Session::switchCustomer(0);
			$MiconisTxModel = M("miconis_tixian");
			$QuanUserModel = M("quan_user");
			Session::switchCustomer($cid);
			$cid = C("PLANT_CUSTOMERID");
			$data['completeTime'] = date("Y-m-d H:i:s");
			$data['state'] = 1;
			$where['tx.id'] = $id;
			$oldarr = $MiconisTxModel->alias('tx')
					->join("wx_all_user AS alu ON alu.id=tx.uId")
					->join("wx_".$cid."_user_weixin_info AS wlx ON wlx.id=alu.weixinUserId")
					->join("LEFT JOIN  wx_quan_user as qu ON qu.uid = tx.uId")
					->where($where)->find();
					// echo $MiconisTxModel->getlastSql();
		/* 	if ($oldarr['miCoins']< $oldarr['cash']) {
				Output::instance()->resultCode(code_product_refund_failed_miCoins);
				return;
			} */
			Log::write("红包信息：". $MiconisTxModel->getlastSql());
			$MiconisTxModel->startTrans();
			$rollback1 = WeixinApi::transferAccountsByOpenId($oldarr['openid'], $oldarr['cash'],'销售佣金，加油！');
			// 原来提现方式采用红包接口
			// $rollback1 = WeixinApi::sendRedEnvelopeByOpenId('密友圈',$oldarr['openid'], $oldarr['cash'],'销售佣金，加油！');
			Log::write("红包信息：". $oldarr['openid'] ."---". $oldarr['cash']);
			if ($oldarr && $rollback1) {
				$rollback2 = $MiconisTxModel->alias('tx')->where($where)->save($data);
				Log::write("存入提现状态:".$MiconisTxModel->getLastSql());
			}
			$qwhere['uid'] = $oldarr['uId'];
			$qwhere['qid'] = $oldarr['quanId'];
			$cash = $oldarr['cash'];
			Log::write("提现sql错误：". $rollback1 ."---". $rollback2);
			if ($rollback1&&$rollback2) {
				$MiconisTxModel->commit();
				Output::instance()->resultCode(code_success);
			}else{
				$MiconisTxModel->rollback();
				Output::instance()->resultCode(code_sql_error);
			}
		}
		
}